package finbudget.client.lists;

import com.gwtplatform.dispatch.shared.ActionException;

import finbudget.client.lists.GetListOfItems.ListType;
import finbudget.client.lists.GetListOfItems.Type;

/**
 * List of items that is retrieved from a map-table. For example, table Project-Item table (base of the table is mapBase)
 * stores project-item pairs. When the project is selected a user expects to get filtered list of budget items.
 * So keyField is the name of the project field (fieldProject) in the project-item table;
 * targetField is the name of the budget items field (fieldBudgetItems) in the project-item table;
 * base is the budget items list base; keyValue is the selected project. baseName is the first part of the list name
 * (ea "Project-Items").
 * @author vlakadm
 *
 */
public class MappedListType extends ListType implements ListTypeElement {
	private static final long serialVersionUID = -6926465665484123408L;

	private String baseName;
	private String keyValue;
	private String mapBase;
	private String keyField;
	private String targetField;

	/** Serialization requirements */
	public MappedListType() {
		super();
	}

	public MappedListType(String baseName, String keyValue, String base, String mapBase, String keyField, String targetField) {
		this.type = Type.COMPOSITE;
		this.baseName = baseName;
		this.keyValue = keyValue;
		this.base = base;
		this.mapBase = mapBase;
		this.keyField = keyField;
		this.targetField = targetField;
	}

	@Override
	public MappedListType clone() {
		MappedListType result = new MappedListType();
		ListType type = super.clone();
		result.base = type.base;
		result.type = type.type;
		result.baseName = baseName == null ? null : new String(baseName);
		result.keyValue = keyValue == null ? null : new String(keyValue);
		result.mapBase = mapBase == null ? null : new String(mapBase);
		result.keyField = keyField == null ? null : new String(keyField);
		result.targetField = targetField == null ? null : new String(targetField);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		boolean equals = super.equals(obj);
		if (equals) {
			equals = obj != null && obj instanceof MappedListType;
			if (equals) {
				MappedListType object = (MappedListType) obj;
				if (keyValue != null) {
					equals = keyValue.equals(object.keyValue);
				} else {
					equals = object.keyValue == null;
				}
				if (equals) {
					if (mapBase != null) {
						equals = mapBase.equals(object.mapBase);
					} else {
						equals = object.mapBase == null;
					}
					if (equals) {
						if (keyField != null) {
							equals = keyField.equals(object.keyField);
						} else {
							equals = object.keyField == null;
						}
						if (equals) {
							if (targetField != null) {
								equals = targetField.equals(object.targetField);
							} else {
								equals = targetField == null;
							}
						}
					}
				}
			}
		}
		return equals;
	}

	public String getMapBase() {
		return mapBase;
	}

	public String getKeyValue() {
		return keyValue;
	}

	public String getKeyField() {
		return keyField;
	}

	public String getTargetField() {
		return targetField;
	}

	public String getName() {
		return baseName + "/" + keyValue;
	}

	@Override
	public GetListOfItemsResult getList(ListTypeVisitor visitor, GetListOfItemsAction action) throws ActionException {
		return visitor.getList(this, action);
	}

}
